package so.wy.servicecenter.businessbed.manager.utils;

import so.wy.servicecenter.businessbed.dao.enums.TypePriceEnum;
import so.wy.servicecenter.businessbed.dao.rdb.order.order_price.OrderPriceDO;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author NO
 * @version 0.1.0
 * @since 2018.01.28
 */
public class OrderUtils {
    /**
     * 生成订单号
     *
     * @return 订单号
     * @author NO
     * @since 2019.01.28
     */
    public static String genOrderSerial(long userId) {
        return new SimpleDateFormat("yyMMddHHmmssSSS").format(new Date()) +
                cutStringFromLast(Long.toString(userId), 5);
    }

    //生成预支付订单号
    public static String genPayPreOrderNum() {
        String uuid = UUID.randomUUID().toString();

        return uuid.replace("-", "");
    }


    /**
     * 计算订单费用
     *
     * @param price     计费信息
     *
     * @param useLength 使用时长（秒）
     * @return 费用
     */
    public static BigDecimal calcOrderFee(OrderPriceDO price, int useLength) {
        //首先将秒转换成小时
        int hour = useLength / 60 / 60;
        int minute = useLength / 60 - hour * 60;
        hour = minute > 0 ? (hour + 1) : hour;
        if (hour == 0 && useLength > 0) {
            hour = 1;
        }

        if (TypePriceEnum.NORMAL.getCode().equals(price.getPriceType())) {
            if (hour <= price.getPriceStartHour()) {
                return price.getPriceStartMoney();
            } else {
                return price.getPriceStartMoney().add(price.getPriceAfterMoney()
                        .multiply(new BigDecimal((hour - price.getPriceStartHour()))));
            }
        } else {
            int zqCount = hour / price.getPriceStartHour();
            zqCount = hour % price.getPriceStartHour() > 0 ? zqCount + 1 : zqCount;
            return price.getPriceStartMoney().multiply(new BigDecimal(zqCount));
        }
    }

    /**
     * 生成使用时长描述
     *
     * @param useLength 使用时长(秒)
     * @return 使用时长描述
     * @author NO
     * @since 2019.01.29
     */
    public static String generateUseLengthDesc(int useLength) {
        int hour = useLength / 60 / 60;
        int minute = useLength / 60 - hour * 60;
        int second = useLength - hour * 60 * 60 - minute * 60;

        StringBuilder result = new StringBuilder();
        if (hour > 0) {
            result.append(String.format("%s小时", hour));
        }

        result.append(String.format("%s分", second > 0 ? minute + 1 : minute));
        return result.toString();
    }

    /**
     * 根据位数从后截取字符串，位数不足的前边用0补齐
     *
     * @param param 字符串
     * @param digit 位数
     * @author NO
     * @since 2018.09.19
     **/
    private static String cutStringFromLast(String param, int digit) {
        if (param.length() < digit) {
            StringBuilder stringBuilder = new StringBuilder(param);
            while (stringBuilder.length() < digit) {
                stringBuilder.insert(0, "0");
            }
            return stringBuilder.toString();
        } else {
            return param.substring(param.length() - digit);
        }
    }
}